#include <iostream>
#include <thread>
#include <utility>
#include <future>
#include <vector>
#include <chrono>
#include <string>

// 重写获取当前时间的辅助函数，使用chrono库
std::string getCurrentTime()
{
    using namespace std::chrono;
    auto now = system_clock::now();
    auto duration = now.time_since_epoch();

    // 获取秒数
    auto seconds = duration_cast<std::chrono::seconds>(duration);
    auto sec = seconds.count() % 60;

    // 获取分钟
    auto minutes = duration_cast<std::chrono::minutes>(duration);
    auto min = minutes.count() % 60;

    // 获取小时
    auto hours = duration_cast<std::chrono::hours>(duration);
    auto hr = hours.count() % 24;

    // 获取毫秒
    auto ms = duration_cast<std::chrono::milliseconds>(duration) % 1000;

    // 格式化输出
    char buffer[32];
    sprintf(buffer, "%02ld:%02ld:%02ld.%03ld", hr, min, sec, ms.count());
    return std::string(buffer);
}

// 模拟获取股票价格的函数
double getStockPrice(const std::string &symbol)
{
    std::cout << getCurrentTime() << " [线程 " << std::this_thread::get_id()
              << "] 开始查询 " << symbol << " 的价格\n";
    // 模拟网络延迟
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << getCurrentTime() << " [线程 " << std::this_thread::get_id()
              << "] 完成查询 " << symbol << " 的价格\n";

    // 模拟股票价格计算
    if (symbol == "AAPL")
        return 188.52;
    if (symbol == "GOOGL")
        return 152.23;
    if (symbol == "MSFT")
        return 415.50;
    return 0.0;
}

// 模拟计算股票收益率的函数
double calculateReturn(double initialPrice, double days)
{
    std::cout << getCurrentTime() << " [线程 " << std::this_thread::get_id()
              << "] 开始计算收益率\n";
    // 模拟复杂计算
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << getCurrentTime() << " [线程 " << std::this_thread::get_id()
              << "] 完成计算收益率\n";
    return initialPrice * (1 + 0.001 * days);
}

// 使用packaged_task获取单个股票信息
void getStockInfo(const std::string &symbol)
{
    // 创建packaged_task用于异步获取股票价格
    std::packaged_task<double(const std::string &)> price_task(getStockPrice);
    std::future<double> price_future = price_task.get_future();

    // 在新线程中执行价格查询
    std::thread(std::move(price_task), symbol).detach();

    // 创建另一个future用于计算预期收益
    std::future<double> return_future = std::async(std::launch::async,
                                                   calculateReturn, 100.0, 30);

    std::cout << "正在获取 " << symbol << " 的信息...\n";

    // 等待并获取结果
    std::cout << getCurrentTime() << " 等待并获取结果\n";
    double current_price = price_future.get();
    double expected_return = return_future.get();

    std::cout << symbol << " 当前价格: $" << current_price << std::endl;
    std::cout << symbol << " 30天预期价格: $" << expected_return << std::endl;
    std::cout << "-------------------------\n";
}

int main(void)
{
    std::cout << getCurrentTime() << " [主线程 " << std::this_thread::get_id()
              << "] 程序开始执行\n\n";

    // 使用shared_future异步获取多个股票价格
    std::vector<std::shared_future<double>> price_futures;

    // 为多个股票创建shared_future
    auto google_future = std::async(std::launch::async, getStockPrice, "GOOGL").share();
    auto microsoft_future = std::async(std::launch::async, getStockPrice, "MSFT").share();

    price_futures.push_back(google_future);
    price_futures.push_back(microsoft_future);

    // 使用封装的函数获取Apple股票信息
    getStockInfo("AAPL");

    // 获取其他股票的价格
    std::cout << "其他股票价格：\n";
    std::cout << "Google股票价格: $" << price_futures[0].get() << std::endl;
    std::cout << "Microsoft股票价格: $" << price_futures[1].get() << std::endl;

    return 0;
}